package util

import (
	"context"
	"github.com/go-kit/kit/endpoint"
	"golang.org/x/time/rate"
	"errors"
)

//限流
func RateLimit(limit *rate.Limiter) endpoint.Middleware{
	return func (next endpoint.Endpoint) endpoint.Endpoint{
		return func(ctx context.Context, request interface{}) (response interface{}, err error){
			if !limit.Allow(){
				return nil, errors.New("too many request")
			}
			return next(ctx, request)
		}
	}
}